/*
package com.qiwx.pigeon.fragment.produce;

import android.content.Intent;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DBSqlUtil;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.activity.produce.AddLayEggDateProduceActivity;
import com.qiwx.pigeon.adapter.produce.CheckEggAdapter;
import com.qiwx.pigeon.base.Constant;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.db.CageModel;
import com.qiwx.pigeon.data.vo.CageBean;
import com.qiwx.pigeon.fragment.AbsQueryFragment;
import com.qiwx.pigeon.fragment.ProduceFragment;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.GridRadioGroup;
import com.qiwx.pigeon.widget.itemderection.CustomGridItemDecoration;

import org.angmarch.views.NiceSpinner;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_END;
import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_START;

*/
/**
 * @description
 * @Author qiwx
 * @Date 2020-03-29 16:52
 * 查仔工段
 **//*

public class CheckBabyOperationFragment extends AbsQueryFragment {
    @BindView(R.id.spinner_wh)
    NiceSpinner spinnerWh;
    @BindView(R.id.txt_start_date)
    TextView txtStartDate;
    @BindView(R.id.txt_end_date)
    TextView txtEndDate;
    @BindView(R.id.btn_condition)
    Button btnCondition;
    @BindView(R.id.btn_query)
    Button btnQuery;
    @BindView(R.id.rel_query_tools_top)
    RelativeLayout relQueryToolsTop;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.rel_nodata_recycleview)
    RelativeLayout relNodataRecycleview;
    @BindView(R.id.arrow_left)
    ArrowView arrowLeft;
    @BindView(R.id.txt_prepage)
    TextView txtPrepage;
    @BindView(R.id.txt_divider)
    TextView txtDivider;
    @BindView(R.id.txt_nextpage)
    TextView txtNextpage;
    @BindView(R.id.arrow_right)
    ArrowView arrowRight;
//    @BindView(R.id.rb_chuke1)
//    RadioButton rbChuke1;
//    @BindView(R.id.rb_chuke2)
//    RadioButton rbChuke2;
    @BindView(R.id.rb_chuke3)
    RadioButton rbChuke3;
    @BindView(R.id.rb_wck)
    RadioButton rbWCK;
    @BindView(R.id.rb_no_egg_baby)
    RadioButton rbNoEggBaby;
    @BindView(R.id.group_check_baby)
    RadioGroup groupCheckBaby;
    @BindView(R.id.rb_fd)
    RadioButton rbFd;
    @BindView(R.id.rb_empty)
    RadioButton rbEmpty;
    @BindView(R.id.group_cage_status)
    RadioGroup groupCageStatus;
    @BindView(R.id.btn_opreation_ok)
    Button btnOpreationOk;
    @BindView(R.id.btn_opreation_add_date)
    Button btnOpreationAddDate;
    int produceStatus;//生产状态
    int pageNo = 1;//页码
    int pageAll;//总页码
    int pageSize = 20;//每页显示10条记录
    int allNums = 0;//总共有多少条记录
    List<CageBean> data = new ArrayList<>();
    CheckEggAdapter adapter;
    public int selectPos = -1;
    int beforeDays = 7;//默认查蛋日期距离今天天数
    @BindView(R.id.sw_remind)
    SwitchCompat swRemind;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_check_baby;
    }

    @Override
    public void initAfterOnCreate() {
        initView();
        initSpinner();


    }

    private void initView() {
        spinnerWh.setText("选择状态");
        btnCondition.setVisibility(View.GONE);
        arrowLeft.changeArrowLeft();
        arrowRight.changeArrowRight();
        //查蛋页面默认查的是今天往前推7天
        Date startDate = DateTimeUtils.addDay(new Date(), -beforeDays);
        txtStartDate.setText(DateTimeUtils.date2String(startDate));
        Date endDate = DateTimeUtils.addDay(new Date(), 0);
        txtEndDate.setText(DateTimeUtils.date2String(endDate));
        recyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        recyclerview.addItemDecoration(new CustomGridItemDecoration(1));
        adapter = new CheckEggAdapter(R.layout.adapter_check_egg_list_item, data);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                updateListSelected(position);
                selectPos = position;
                swRemind.setChecked(true);
            }
        });
        initEvent();
    }

    private void initEvent() {
//        rbEmpty.setOnCheckedChangeListener((btn, ischeck) -> {
//            if (ischeck) {
//                groupCheckBaby.setVisibility(View.GONE);
//            } else {
//                groupCheckBaby.setVisibility(View.VISIBLE);
//            }
//        });
        swRemind.setOnCheckedChangeListener((btn, ischeck) -> {
            if (ischeck) {
                btn.setText("关闭提醒");
            } else {
                btn.setText("打开提醒");

            }
        });

    }

    //更新列表选择状态
    public void updateListSelected(int pos) {
        for (int i = 0; i < data.size(); i++) {
            if (pos == i) {
                data.get(i).setSelected(true);
            } else {
                data.get(i).setSelected(false);
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initSpinner() {
        Map<String, Integer> maps = CageStatusConfig.getProduceStatus();
        List<String> data = new ArrayList<>();
        for (String v : maps.keySet()) {
            data.add(v);
        }
        spinnerWh.attachDataSource(data);
        spinnerWh.setOnSpinnerItemSelectedListener((parent, view, position, id) -> {
            produceStatus = maps.get(data.get(position));

        });
        //查仔模块先查查仔状态
        spinnerWh.setSelectedIndex(4);
        produceStatus = maps.get(data.get(4));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_START:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra("car_Time_MSG"))) {
                    txtStartDate.setText(data.getStringExtra("car_Time_MSG"));
                }
                break;
            case REQUEST_CODE_END:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra("car_Time_MSG"))) {
                    txtEndDate.setText(data.getStringExtra("car_Time_MSG"));
                }
                break;
        }
    }

    // 查询有多少条记录
    private void queryCount(String wh_id, long start, long end, int status) {
        String sql = "select count(*) as count from producemodel where ";
        String condition = "wh_id=%s and lay_egg_date>=%d and lay_egg_date<=%d and producestatus=%d";
        condition = String.format(condition, wh_id, start, end, status);
        sql += condition;

        Cursor cursor = LitePal.findBySQL(sql);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int index = cursor.getColumnIndex("count");
                if (index >= 0) {
                    allNums = cursor.getInt(0);
                }
            }
        }
        if (cursor != null) {
            cursor.close();
        }
        pageAll = allNums / pageSize;
        if (allNums % pageSize != 0) {
            pageAll += 1;
        }
        txtPrepage.setText(pageNo + "");
        txtNextpage.setText(pageAll + "");
    }

    private void goPage(int index) {
        int offset = 0;
        if (index <= pageAll) {
            offset = (index - 1) * pageSize;
        }
        String startDate = txtStartDate.getText().toString();
        String endDate = txtEndDate.getText().toString();
        if (startDate == null || endDate == null) {
            MyToast.showLong("请选择查询日期");
            return;
        }
        startDate += " " + DateTimeUtils.getCurrentTime();
        endDate += " " + DateTimeUtils.getCurrentTime();
        String condition = "wh_id=? and lay_egg_date>=? and lay_egg_date<=? and producestatus=?";
        String wh_id = MainActivity.wh_id + "";
        long start = DateTimeUtils.date2TimeStamp(startDate);
        long end = DateTimeUtils.date2TimeStamp(endDate);
        if (start > end) {
            MyToast.showLong("开始日期不能晚于结束日期");
            return;
        }
        if (allNums <= 0) {
            queryCount(wh_id, start, end, produceStatus);
        }
        List<CageModel> list = LitePal.limit(pageSize).offset(offset).
                where(condition, wh_id, start + "", end + "", produceStatus + "")
                .find(CageModel.class);
        if (list != null && list.size() > 0) {
            data.clear();
            for (int i = 0; i < list.size(); i++) {
                CageModel model = list.get(i);
                CageBean cageBean = new CageBean();
                cageBean.setNumber(model.getNumber());
                cageBean.setId(model.getId());
                data.add(cageBean);
            }
            adapter.notifyDataSetChanged();
            txtNodata.setVisibility(View.GONE);
            linNodata.setVisibility(View.GONE);
            recyclerview.setVisibility(View.VISIBLE);
        } else {
            txtNodata.setVisibility(View.VISIBLE);
            linNodata.setVisibility(View.VISIBLE);
            recyclerview.setVisibility(View.GONE);
        }

    }

    //确认操作
    public void save() {
        if (selectPos >= 0 && selectPos < data.size()) {
            CageModel produceModel = LitePal.find(CageModel.class, data.get(selectPos).getId());
            int babyStatus = 0;
            if (produceModel != null) {
                produceModel.setIs_remind(swRemind.isChecked() ? 0 : 1);

                int ckAmount;
//                if (rbChuke1.isChecked()) {
//                    ckAmount = 1;
//                    babyStatus = CageStatusConfig.CHECK_BABY_CK1;
//                } else if (rbChuke2.isChecked()) {
//                    ckAmount = 2;
//                    babyStatus = CageStatusConfig.CHECK_BABY_CK2;
//                } else
               if (rbChuke3.isChecked()) {
                    ckAmount = 3;
                    babyStatus = CageStatusConfig.CHECK_BABY_CK3;
                } else if (rbWCK.isChecked()) {
                    ckAmount = 0;
                    babyStatus = CageStatusConfig.CHECK_BABY_WCK;
                } else if (rbNoEggBaby.isChecked()) {
                    ckAmount = -1;
                    babyStatus = CageStatusConfig.CHECK_BABY_NO_EGG_BABY;
                } else {
                    ckAmount = 1;
                    babyStatus = CageStatusConfig.CHECK_BABY_CK1;
                }
                DBSqlUtil.saveChuKe(produceModel, ckAmount);

                if (rbEmpty.isChecked()) {
                    DBSqlUtil.setCageKW(produceModel,Constant.USER_ID);
                } else {
                    produceModel.setProduceStatus(CageStatusConfig.PRODUCE_CZ2);

                }
                CageBean bean = data.get(selectPos);
                bean.setCheck_baby_status(babyStatus);
                adapter.notifyDataSetChanged();
            }
            if (produceModel.save()) {
                MyToast.showLong("操作成功");
                DBSqlUtil.updateCageStatus(produceModel,Constant.USER_ID);

            }

        } else {
            MyToast.showLong("请选择鸽笼！");
        }
    }

    //跳转补录下蛋日期
    private void goAddLayEggDate() {
        Intent go = new Intent();
        go.setClass(getActivity(), AddLayEggDateProduceActivity.class);
        if (selectPos >= 0 && selectPos < data.size()) {
            go.putExtra(Constant.INTENT_C_ID, data.get(selectPos).getId());
            go.putExtra(Constant.INTENT_C_NUMBER, data.get(selectPos).getNumber());
            go.putExtra(Constant.INTENT_WH_ID, MainActivity.wh_id);
            go.putExtra(Constant.INTENT_WH_NUMBER, MainActivity.wh_number);
        }
        startActivity(go);
    }

    @OnClick({R.id.txt_start_date, R.id.txt_end_date, R.id.btn_query, R.id.btn_opreation_ok, R.id.btn_opreation_add_date, R.id.arrow_left, R.id.arrow_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_start_date:
                selectStartValiDate();
                break;
            case R.id.txt_end_date:
                selectEndValiDate(txtStartDate.getText().toString());
                break;
            case R.id.btn_query:
                goPage(1);
                break;
            case R.id.btn_opreation_ok:
                save();
                break;
            case R.id.btn_opreation_add_date:
                goAddLayEggDate();
                break;
            case R.id.arrow_left:
                if (pageNo == 1) {
                    MyToast.showShort("已经是第一页");
                } else {
                    pageNo = pageNo - 1;
                    goPage(pageNo);
                    txtPrepage.setText(pageNo + "");

                }
                break;
            case R.id.arrow_right:
                if (pageNo == pageAll) {
                    MyToast.showShort("已经是最后一页");
                } else {
                    pageNo = pageNo + 1;
                    goPage(pageNo);
                    txtPrepage.setText(pageNo + "");

                }

                break;
        }
    }
}
*/
